package org.example.controllers;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.common.NumberCheck;
import org.example.entitys.managers.Student;
import org.example.entitys.systems.User;
import org.example.service.inter.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/24
 */
@Controller
@Slf4j
@ResponseBody
@RequestMapping(value = "/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/create")
    public ApiResponse createStudent(String name, String from_place, String parent_phone, String login_id, String comment) {
        if (NumberCheck.isNotNumber(login_id)) return ApiResponse.paramIncorrect("login_id");
        Student student = new Student(-1, name, from_place, parent_phone, "大一",
                0, Integer.parseInt(login_id), comment);
        log.info("addStudent(student = " + student + ")");
        return studentService.addStudent(student) ? ApiResponse.ok(student) : ApiResponse.failure();
    }

    @GetMapping("/excel-export")
    public void export(HttpServletResponse response, @RequestParam("file_name") String fileName) {
        // set response properties
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") ;
        response.setCharacterEncoding("utf-8") ;
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx") ;
        // write
        List<Student> userList = studentService.getAllStudents();
        try {
            EasyExcel.write(response.getOutputStream())
                    .head(Student.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("用户信息")
                    .doWrite(userList);
        }  catch(IOException e) {
            log.error("文件导出错误。") ;
        }
    }
    @PostMapping("/excel-import")
    @ResponseBody
    public ApiResponse excelImport(@RequestPart("file") MultipartFile file) {
        try{
            List<Student> userList = EasyExcel.read(file.getInputStream())
                    .head(Student.class)
                    .sheet()
                    .doReadSync();
            return ApiResponse.ok("返回结果:",userList);//为了不污染数据库直接输出
            //return studentService.inputStudents(userList) ? ApiResponse.ok(file) : ApiResponse.failure();
        } catch (IOException e) {
            log.error("import file error!");
            return ApiResponse.failure() ;
        }
    }

    @PostMapping("/check")
    public ApiResponse checkStudents(int[] id) {//审核学生
        //审核多个学员
        log.info("checkStudents(id = " + Arrays.toString(id) + ")");
        for (int i : id) studentService.checkStudent(i);
        return ApiResponse.ok(id);
    }
}
